#!/usr/local/bin/python3
# -*- coding:utf8 -*-
# Author Aaron 
# 2017-05-11 21:58:48
import socket

forward = "1"
back = "2"
stop = "3"
left = "4"
right = "5"


def run_raspberry():
    commands = {'forward': 1, 'back': 2, 'stop': 3, 'left': 4, 'right': 5}
    # HOST = '127.0.0.1'
    HOST = '192.168.1.7'
    PORT = 9999
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 默认也是这些参数
    s.bind((HOST, PORT))
    s.listen(5)
    while True:
        sock, addr = s.accept()
        msg = sock.recv(1024)
        msg = msg.decode("utf-8")
        print("msg :", msg)
        print("msg len :", len(msg))
        print("msg type :", type(msg))
        if opt_car(msg):
            print("this is command")
        else:
            print("this is not command")
        sock.send("服务器发送到客户端".encode("utf-8"))
        sock.close()


def opt_car(opt):
    if opt == forward:
        print("this is forward")
        return True
    elif opt == back:
        print("this is back")
        return True
    elif opt == stop:
        print("this is stop")
        return True
    elif opt == left:
        print("this is left")
        return True
    elif opt == right:
        print("this is right")
        return True

    return False


def car_forward():
    pass


def car_back():
    pass


def car_stop():
    pass


def car_left():
    pass


def car_right():
    pass


def car_left():
    pass


if __name__ == '__main__':
    run_raspberry()
